package com.htl.exampro.course.controller;

import com.htl.exampro.base.model.ServerResponse;
import com.htl.exampro.course.pojo.vo.ChapterVO;
import com.htl.exampro.course.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    // 获取课程所有章节
    @GetMapping("/{courseId}")
    public ServerResponse<List<ChapterVO>> getChapters(@PathVariable("courseId") Long courseId) {
        List<ChapterVO> chapterVOS = chapterService.getChaptersByCourseId(courseId);
        return ServerResponse.success(chapterVOS);
    }

    @PostMapping("/")
    public ServerResponse<Void> saveChapter(@RequestBody ChapterVO detailVOS) {
        chapterService.saveChapter(detailVOS);
        return ServerResponse.success();
    }

    @PutMapping("/")
    public ServerResponse<Void> updataChapter(@RequestBody ChapterVO detailVOS) {
        chapterService.updataChapter(detailVOS);
        return ServerResponse.success();
    }

    // 删除单个章节
    @DeleteMapping("/{chapterId}")
    public ServerResponse<Void> deleteChapter(@PathVariable("chapterId") Long chapterId) {
        chapterService.deleteChapter(chapterId);
        return ServerResponse.success();
    }

    // 批量保存/更新章节
    @PostMapping("/{courseId}")
    public ServerResponse<Void> saveOrUpdateChapters(@PathVariable("courseId") Long courseId, @RequestBody List<ChapterVO> detailVOS) {
        chapterService.saveOrUpdateChapters(courseId, detailVOS);
        return ServerResponse.success();
    }


}